$(function(){
	var pageIndex = 1;
	var deleteNode;
	var changeId = null;
	var add = 0;
	var teacherId = 0;
	var pageNum = 1;
	var totalPages = 1;
	//加载--教师信息请求成功
	function requestSuccess(data){
		if( data.status == true ){
			// alert(data.content);
			if( data.data.content.length == 0 ){
				alert("无老师记录");
				return;
			}
			totalPages = data.data.totalPages;
			if( data.data.totalPages ){
				$("#page-total").html(data.data.totalPages);
			}else{
				$("#page-total").html(1);
			}
			$("#page-num").html(pageNum);

			var node2 = $(".detail-check").find("tr").eq(0).clone();
			$(".detail-check").html("");

			for( var i = 0; i< data.data.content.length; i++ ){
				var item = data.data.content[i];
				var node = node2.clone();
				
				node.find(".teacher-id").html(item.id);
				node.find(".teacher-name").html(item.name);
				node.find(".teacher-gender").html(item.gender);
				node.find(".teacher-age").html(item.age);
				node.find(".teacher-idCard").html(item.idCard);
				node.find(".teacher-phone").html(item.phone);
				node.find(".teacher-entryTime").html(item.entryTime);	
				node.find(".teacher-subject").html(item.subject);		
				node.removeClass("hide");

				$(".detail-check").append(node);
				console.log(i);
			}
		}else{
			alert("获取老师信息失败");
		}
	}

	//加载--教师信息请求失败
	function requestError(data){
		alert("获取老师信息失败 error");
	}
	//添加--教师请求成功
	function addSalerS(data){
		if( data.status == true ){
			alert("添加成功");
			location.reload();
		}else{
			alert(data.msg+"添加老师失败");
		}
	}
	//添加教师请求失败
	function addSalerE(data){
		alert("添加失败 error");
	}
	//删除教师请求成功
	function deleteSuccess(data){
		if( data.status == true ){
			alert("删除成功!");
			location.reload();
		}else{
			alert("删除失败");
		}
	}
	//删除教师请求失败
	function deleteError(data){
		alert("删除失败Error");
	}
	//搜索教师请求成功
	function findSuccess(data){
		if( data.status ){
	  	var node2 = $(".detail-check").find("tr").eq(0).clone();
	  	$(".detail-check").html("");
	  	// for( var i = 0; i< data.data.content.length; i++ ){
				var item = data.data.content[0];
				var node = node2.clone();
				
				node.find(".teacher-id").html(item.id);
				node.find(".teacher-name").html(item.name);
				node.find(".teacher-subject").html(item.subject);
				node.find(".teacher-sex").html(item.gender);
				node.find(".teacher-age").html(item.age);
				node.find(".teacher-idCard").html(item.idCard);
				node.find(".teacher-phone").html(item.phone);
				node.find(".teacher-entryYear").html(item.entryTime);
				
				node.removeClass("hide");
				$(".detail-check").append(node);
			// }
		}
		
	}

	//搜索教师请求失败
	function findError(data){
		console.log(data.msg);
	}

	//修改教师信息信息
	function setInfoSuccess(data){
		if( data.status == true ){
			alert("修改成功");
			location.reload();
		}else{
			alert("修改失败");
		}
	}
	function setInfoError(data){
		alert("修改失败+error");
	}

	//--获取教师信息成功
	function getInfoSuccess(data){
		
		if( data.status == true ){
			$(".teacher-id2").html(data.data.content[0].id);
			$(".teacher-name2").html(data.data.content[0].name);
			// $(".saler-psw2").html();
			$(".teacher-gender2").html(data.data.content[0].gender);
			$(".teacher-age2").html(data.data.content[0].age);
			$(".teacher-idCard2").html(data.data.content[0].idCard);
			$(".teacher-phone2").html(data.data.content[0].phone);
			$(".teacher-entryTime2").html(data.data.content[0].entryTime);
			$(".teacher-subject2").html(data.data.content[0].subject);
			$(".table-box").addClass("hide");
			$(".account-box").removeClass("hide");
			// $(".table-box2").removeClass("hide");
		}
	}
	//--获取教师信息失败
	function getInfoError(data){
			if( data.status == false ){
				alert("获取失败");
			}
	}


	//--点击修改教师信息
	$(".info-modify").click(function(){
		modify = 1;
		$(".teacher-info").each(function(){
			// $(this).attr("contenteditable", "true");
			$(this).attr("contenteditable", "true");
			$(this).addClass("compile-box");
		})
		$(".add-record").addClass("hide");
		$(".book-class").addClass("hide");
		$(".info-modify").addClass("hide");
		$(".info-back").addClass("hide");
		$(".saler-confirm").removeClass("hide");
		$(".add-back").removeClass("hide");
	});

	//--点击教师查看信息
	$(".detail-check").on("click",".teacher-record",function(){
		// console.log("click");
		add= 0;
		cancel = true;
		console.log($(this).find("th:first").text());
		$(this).click(function(){
			 teacherId = $(this).find("th:first").text();
			 $.ajax({
				type: "get",
				url: 'http://106.75.143.27/teachers',
				// data: {"counselorsId":counselorsId},
				data: {"page":0,"id":teacherId},
				contentType: 'application/x-www-form-urlencoded',
				success: getInfoSuccess,
				error: getInfoError
			});
			
		});
	});


	//--取消修改学员信息
	$(".add-back").click(function(){
		$(".teacher-info").each(function(){
			$(this).attr("contenteditable", "false");
			$(this).removeClass("compile-box");
		});
		// $(".teacher-info").attr("contenteditable", "false").removeClass("compile-box");
		if( add ){//取消添加
			add = 0;
			$(".table-box1").removeClass("hide");
			$(".account-box").addClass("hide");
			$(".table-box2").addClass("hide");

			// $(".add-record").addClass("hide");
			// $(".book-class").removeClass("hide");
			$(".info-modify").removeClass("hide");
			$(".info-back").removeClass("hide");
			$(".saler-confirm").addClass("hide");
			$(".add-back").addClass("hide");
			return;
		}else{//取消修改
			// $(".add-record").removeClass("hide");
			$(".info-modify").removeClass("hide");
			$(".info-back").removeClass("hide");
			$(".saler-confirm").addClass("hide");
			$(".add-back").addClass("hide");
		}
		
	})

	//--返回教师记录页面
	$(".info-back").click(function(){
		$(".table-box1").removeClass("hide");
		$(".account-box").addClass("hide");
		$(".table-box2").addClass("hide");
	});


	//--搜索教师
	$("#select-saler").click(function(){
		var searchText = $("#search-number").val();
		console.log(searchText);
		$.ajax({
			type: "get",
			url: 'http://106.75.143.27/teachers',
			data: {"page":0,"name":searchText},
			contentType: 'application/x-www-form-urlencoded',
			success: findSuccess,
			error: findError
		});
	})

	//--添加教师
	$("#add-teacher").click(function(){
		add = 1;
		$(".table-box1").addClass("hide");
		$(".account-box").removeClass("hide");
		$(".teacher-info").each(function(){
			// $(this).attr("contenteditable", "true");
			$(this).html("");
			$(this).attr("contenteditable", "true");
			$(this).addClass("compile-box");
		});
		$(".teacher-info").html("").attr("contenteditable", "true").addClass("compile-box");
		$(".add-record").addClass("hide");
		// $(".book-class").addClass("hide");
		$(".info-modify").addClass("hide");
		$(".info-back").addClass("hide");
		$(".saler-confirm").removeClass("hide");
		$(".add-back").removeClass("hide");
		
	});
	//--确认添加/修改教师
	$(".saler-confirm").click(function(){
		if(add){
			console.log("add");
			add = 0;
			var data = {
				"name": $(".teacher-name2").html(),
				"password": $(".teacher-psw2").html(),
				"phone": $(".teacher-phone2").html(),
				"gender": $(".teacher-gender2").html(),
				"age": $(".teacher-age2").html(),
				"idCard": $(".teacher-idCard2").html(),
				"entryTime": $(".teacher-entryTime2").html(),
				"subject":$(".teacher-subject2").html()
			};
			console.log(data);
			
			Ajax({
				url: "http://106.75.143.27/teachers",
				type: "post",
				data: JSON.stringify(data),
				contentType: 'application/json',
				dataType: 'json',
				call1:  addSalerS,
				call2:  addSalerE
			});
		}else{
			add = 0;
			console.log("modify");
			var data = {
				
				"name": $(".teacher-name2").html(),
				"password": $(".teacher-psw2").html(),
				"phone": $(".teacher-phone2").html(),
				"gender": $(".teacher-sex2").html(),
				"age": $(".teacher-age2").html(),
				"idCard": $(".teacher-idCard2").html(),
				"entryTime": $(".teacher-entryYear2").html(),
				"subject":$(".teacher-subject2").html()
			};
			console.log(data);
			$.ajax({
				type: "put",
				url: "http://106.75.143.27/teachers/"+teacherId,
				data: JSON.stringify(data),
				contentType: 'application/json',
				dataType: 'json',
				success: setInfoSuccess,
				error: setInfoError
			});
		}
		
	});
	//--删除教师
	$(".detail-check").on("click","#delete-info",function(){
		 teacherId = $(this).parent().find("th:first").text();
		 console.log(teacherId);
		 $.ajax({
			type: "delete",
			url: 'http://106.75.143.27/teachers/'+teacherId,
			// contentType: 'application/x-www-form-urlencoded',
			success: deleteSuccess,
			error: deleteError
		});		
		 return false;
		
	});
	//前一页
	$("#page-pre").click(function(){
		 pageNum = $("#page-num").html();
		 
		if( pageNum == 1 ){
			alert("已经是第1页了哦");
		}else{
			 pageNum--;
			 var num = pageNum -1;
			 console.log(num);
			$.ajax({
				type: "get",
				url: 'http://106.75.143.27/teachers?sort=createdTime,desc',
				data: {"page":num},
				contentType: 'application/x-www-form-urlencoded',
				success: requestSuccess,
				error: requestError
			});
		}
	});
	//--后一页
	$("#page-next").click(function(){
		
		 pageNum = $("#page-num").html();
		
		if( pageNum >= (totalPages) ){
			alert("已经是最后一页了哦");
		}else{
			pageNum++;
			var num = pageNum-1;
			console.log(num);
			$.ajax({
				type: "get",
				url: 'http://106.75.143.27/teachers?sort=createdTime,desc',
				data: {"page":num},
				contentType: 'application/x-www-form-urlencoded',
				success: requestSuccess,
				error: requestError
			});
		}
	});
	//--跳转页面
	$("#skip").click(function(){
		var num = $("#js-skip").val();
		console.log(num);
		if( ( num < 1 )||( num > totalPages ) ){
			alert("请输入正确页数");
		}else{
			$.ajax({
				type: "get",
				url: 'http://106.75.143.27/teachers?sort=createdTime,desc',
				data: {"page":num-1},
				contentType: 'application/x-www-form-urlencoded',
				success: requestSuccess,
				error: requestError
			});
		}
	});

	//加载教师记录
	$.ajax({
		type: "get",
		url: 'http://106.75.143.27/teachers?sort=createdTime,desc',
		data: {"page":0},
		contentType: 'application/x-www-form-urlencoded',
		success: requestSuccess,
		error: requestError
	});
})